package com.gather.treesets;

import com.gather.orm.Person;

import java.util.TreeSet;

public class Example02 {
    public static void main(String[] args) {
        TreeSet<Person> people = new TreeSet<>();
        Person p1 = new Person("xyz",20);
        Person p2 = new Person("abc",24);
        Person p3 = new Person("hello",25);
        Person p4 = new Person("hello",20);
        people.add(p1);
        people.add(p2);
        people.add(p3);
        people.add(p4);
        /**
         * person数据 [name abc : 24, name hello : 20, name hello : 25, name xyz : 20]
         * hello 同姓名，比较时
         * 年龄小的排在前面
         */
        System.out.println("person数据 "+people);
        // 测试能否添加重复元素
        Person p5 = new Person("hello",25);
        people.add(p5);// 无法添加 因为重写了compareTo方法
        System.out.println("person数据 "+people);
        // 删除操作
        people.remove(p3);
        // 另一种删除方式也是可以的 因为实现了 compareTo方法
        people.remove(new Person("abc",24));
        System.out.println("people "+people);// people [name hello : 20, name xyz : 20]
        // 查找
        boolean house = people.contains(new Person("house", 30));
        System.out.println(house?"存在":"不存在");
        boolean contains = people.contains(p1);
        System.out.println(contains?"存在":"不存在");
    }
}
